﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BiocityFilmquizAdmin.Src.Domain;

namespace BiocityFilmquizAdmin.Src.SessionHandle
{
    public class CookieHelper
    {
        private static CookieHelper instance;

        public static CookieHelper getInstance()
        {
            if (instance == null)
            {
                instance = new CookieHelper();
            }
            return instance;
        }

        private CookieHelper()
        {

        }

        public void createLoginCookies(User user, HttpResponse response)
        {
            HttpCookie cookie = new HttpCookie("username");
            cookie.Value = user.name;
            cookie.Expires = DateTime.Now.AddDays(14);
            response.SetCookie(cookie);

            cookie = new HttpCookie("id");
            cookie.Value = user.userID.ToString();
            cookie.Expires = DateTime.Now.AddDays(14);
            response.SetCookie(cookie);
        }

        public void deleteLoginCookies(HttpRequest request, HttpResponse response)
        {
            if (request.Cookies["username"] != null)
            {
                HttpCookie cookie = new HttpCookie("username");
                cookie.Expires = DateTime.Now.AddDays(-1);
                response.Cookies.Add(cookie);

                cookie = new HttpCookie("id");
                cookie.Expires = DateTime.Now.AddDays(-1);
                response.Cookies.Add(cookie);
            }
        }
    }
}